package com.fieldchain.ex.fa100.utils.chart.mk_chart;

import android.content.Context;
import android.graphics.Color;
import android.graphics.drawable.Drawable;
import android.util.AttributeSet;
import android.util.SparseArray;
import com.fieldchain.ex.fa100.utils.chart.auxiliary.axis.MyXAxis;
import com.fieldchain.ex.fa100.utils.chart.auxiliary.MyXAxisRenderer;
import com.fieldchain.ex.fa100.utils.chart.dataset.MkLindataSet;
import com.github.mikephil.charting.charts.LineChart;
import com.github.mikephil.charting.components.YAxis;
import com.github.mikephil.charting.data.LineData;
import com.github.mikephil.charting.interfaces.datasets.ILineDataSet;
import java.text.DecimalFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by biyunlong
 * Email: 1225198434@qq.com
 * Data:  2018/10/4
 * 参考：https://blog.csdn.net/dt235201314/article/details/52222088
 */
public class MarketChart extends LineChart {
    private long minX, maxX;
    private float minY, maxY;
    private List<List<String>> mList = new ArrayList<>();

    public MarketChart(Context context) {
        super(context);
        setChart();
    }

    public MarketChart(Context context, AttributeSet attrs) {
        super(context, attrs);
        setChart();
    }
    public void setChart() {
        getAxisLeft().setDrawGridLines(true);
        getAxisLeft().setGridColor(Color.parseColor("#55B7C2CE"));
        getAxisLeft().setDrawAxisLine(false);
        getAxisLeft().setTextColor(Color.parseColor("#FFB7C2CE"));
        getAxisLeft().setLabelCount(6,true);
        getAxisLeft().setTextSize(9);
        getAxisLeft().enableGridDashedLine(5f, 10f, 0f);
        getAxisRight().setEnabled(false);

        getXAxis().setLabelCount(5,true);

        setTouchEnabled(false);
        setScaleEnabled(false);
        setDrawBorders(false);
        setDescription(null);
        getLegend().setEnabled(false);// 不显示图例
        YAxis axisRight = getAxisRight();
        axisRight.setEnabled(false);
        setMinOffset(0);
        setExtraTopOffset(20);
        setExtraBottomOffset(10);

    }

    public void setValues(Drawable drawable, List<List<String>>list) {
        mList.clear();
        mList.addAll(list);
        MkLindataSet lineDataSet = MkLindataSet.builds(list,drawable);
        //线的集合（可单条或多条线）
        List<ILineDataSet> dataSets = new ArrayList<>();
        dataSets.add(lineDataSet);
        //把要画的所有线(线的集合)添加到LineData
        LineData lineData = new LineData(dataSets);

        minY = lineData.getYMin();
        maxY = lineData.getYMax();

        if(minY==maxY&&minY==0){
            return;
        }
        getAxisLeft().setAxisMinimum(minY);
        //把最终的数据setData
        setData(lineData);
        notifXY();
    }

    @Override
    protected void init() {
        super.init();
        mXAxis = new MyXAxis();
        mXAxisRenderer = new MyXAxisRenderer(mViewPortHandler, getXAxis(), mLeftAxisTransformer, this);
    }

    @Override
    public MyXAxis getXAxis() {
        return (MyXAxis) super.getXAxis();
    }


    public void notifXY() {
        //X 轴
        SparseArray<String> xLabels = new SparseArray<>();
        for (int i = 0; i < mList.size(); i+=6) {
            long value = Long.parseLong(mList.get(i).get(0));
            SimpleDateFormat sdf = new SimpleDateFormat("HH:mm");
            String label = sdf.format(value );
            xLabels.put(i, label);
        }
        getXAxis().setXLabels(xLabels);

    }

    public String m2(float f) {
        DecimalFormat df = new DecimalFormat("#0.00");
        return df.format(f);
    }
}
